package com.aimosheng.video.dialog;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.Constants;
import com.aimosheng.common.HtmlConfig;
import com.aimosheng.common.bean.ConfigBean;
import com.aimosheng.common.dialog.AbsDialogFragment;
import com.aimosheng.common.http.HttpCallback;
import com.aimosheng.common.interfaces.CommonCallback;
import com.aimosheng.common.interfaces.OnItemClickListener;
import com.aimosheng.common.mob.MobBean;
import com.aimosheng.common.mob.MobCallback;
import com.aimosheng.common.mob.MobShareUtil;
import com.aimosheng.common.mob.ShareData;
import com.aimosheng.common.utils.ToastUtil;
import com.aimosheng.video.R;
import com.aimosheng.video.activity.AbsVideoPlayActivity;
import com.aimosheng.video.adapter.VideoShareAdapter;
import com.aimosheng.video.bean.VideoBean;
import com.aimosheng.video.event.VideoShareEvent;
import com.aimosheng.video.http.VideoHttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by cxf on 2018/10/19.
 * 视频分享弹窗
 */

public class VideoShareDialogFragment extends AbsDialogFragment implements OnItemClickListener<MobBean> {

    private RecyclerView mRecyclerView;
    //private RecyclerView mRecyclerView2;
    private VideoBean mVideoBean;

    private MobCallback mMobCallback;
    private MobShareUtil mMobShareUtil;
    private ConfigBean mConfigBean;
    private VideoBean mShareVideoBean;

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_video_share;
    }

    @Override
    protected int getDialogStyle() {
        return R.style.dialog2;
    }

    @Override
    protected boolean canCancel() {
        return true;
    }

    @Override
    protected void setWindowAttributes(Window window) {
        window.setWindowAnimations(R.style.bottomToTopAnim);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.BOTTOM;
        window.setAttributes(params);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle == null) {
            return;
        }
        mVideoBean = bundle.getParcelable(Constants.VIDEO_BEAN);
        if (mVideoBean == null) {
            return;
        }

        CommonAppConfig.getInstance().getConfig(new CommonCallback<ConfigBean>() {
            @Override
            public void callback(ConfigBean bean) {
                mConfigBean = bean;
            }
        });
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
//        mRecyclerView2 = (RecyclerView) findViewById(R.id.recyclerView_2);
//        mRecyclerView2.setHasFixedSize(true);
//        mRecyclerView2.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        List<MobBean> list = null;
        ConfigBean configBean = CommonAppConfig.getInstance().getConfig();
        if (configBean != null) {
            list = MobBean.getVideoShareTypeList(configBean.getVideoShareTypes());
        }
        if (list != null) {
            VideoShareAdapter adapter = new VideoShareAdapter(mContext, list);
            adapter.setOnItemClickListener(this);
            mRecyclerView.setAdapter(adapter);
        }

//        List<MobBean> list2 = new ArrayList<>();
//        MobBean linkBean = new MobBean();
//        linkBean.setType(Constants.LINK);
//        linkBean.setName(R.string.copy_link);
//        linkBean.setIcon1(R.mipmap.icon_share_video_link);
//        list2.add(linkBean);
//        MobBean reportBean = new MobBean();
//        if (mVideoBean.getUid().equals(CommonAppConfig.getInstance().getUid())) {//自己的视频
//            reportBean.setType(Constants.DELETE);
//            reportBean.setName(R.string.delete);
//            reportBean.setIcon1(R.mipmap.icon_share_video_delete);
//        } else {
//            reportBean.setType(Constants.REPORT);
//            reportBean.setName(R.string.report);
//            reportBean.setIcon1(R.mipmap.icon_share_video_report);
//        }
//        list2.add(reportBean);
//        MobBean saveBean = new MobBean();
//        saveBean.setType(Constants.SAVE);
//        saveBean.setName(R.string.save);
//        saveBean.setIcon1(R.mipmap.icon_share_video_save);
//        list2.add(saveBean);
//        VideoShareAdapter adapter2 = new VideoShareAdapter(mContext, list2);
//        adapter2.setOnItemClickListener(this);
//        mRecyclerView2.setAdapter(adapter2);
    }

    @Override
    public void onItemClick(MobBean bean, int position) {
        if (!canClick()) {
            return;
        }
        dismiss();
        switch (bean.getType()) {
            case Constants.LINK://复制链接
                ((AbsVideoPlayActivity) mContext).copyLink(mVideoBean);
                break;
            case Constants.REPORT://举报
                //VideoReportActivity.forward(mContext, mVideoBean.getId());
                break;
            case Constants.SAVE://保存
                ((AbsVideoPlayActivity) mContext).downloadVideo(mVideoBean);
                break;
            case Constants.DELETE://删除
                ((AbsVideoPlayActivity) mContext).deleteVideo(mVideoBean);
                break;
            default:
                if (mContext instanceof AbsVideoPlayActivity){
                    ((AbsVideoPlayActivity) mContext).shareVideoPage(bean.getType(), mVideoBean);
                }else if(mContext instanceof AppCompatActivity){
                    shareVideoPage(bean.getType(), mVideoBean);
                }
                break;
        }
    }



    /**
     * 分享页面链接
     */
    public void shareVideoPage(String type, VideoBean videoBean) {
        if (videoBean == null || mConfigBean == null) {
            return;
        }
        if (mMobCallback == null) {
            mMobCallback = new MobCallback() {

                @Override
                public void onSuccess(Object data) {
                    if (mShareVideoBean == null) {
                        return;
                    }
                    VideoHttpUtil.setVideoShare(mShareVideoBean.getId(), new HttpCallback() {
                        @Override
                        public void onSuccess(int code, String msg, String[] info) {
                            if (code == 0 && info.length > 0 && mShareVideoBean != null) {
                                JSONObject obj = JSON.parseObject(info[0]);
                                EventBus.getDefault().post(new VideoShareEvent(mShareVideoBean.getId(), obj.getString("nums")));
                            } else {
                                ToastUtil.show(msg);
                            }
                        }
                    });
                }

                @Override
                public void onError() {

                }

                @Override
                public void onCancel() {

                }

                @Override
                public void onFinish() {

                }
            };
        }
        mShareVideoBean = videoBean;
        ShareData data = new ShareData();
        data.setTitle(mConfigBean.getVideoShareTitle());
        data.setDes(mConfigBean.getVideoShareDes());
        data.setImgUrl(videoBean.getUserBean().getAvatarThumb());
        String webUrl = HtmlConfig.SHARE_VIDEO + videoBean.getId();
        data.setWebUrl(webUrl);
        if (mMobShareUtil == null) {
            mMobShareUtil = new MobShareUtil();
        }
        mMobShareUtil.execute(type, data, mMobCallback);
    }


}
